package com.sambo.inspection.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.JsonElement;
import com.lzy.okgo.model.Response;
import com.sambo.inspection.R;
import com.sambo.inspection.activity.support.BaseActivity;
import com.sambo.inspection.adapter.CommonAdapter;
import com.sambo.inspection.adapter.ViewHolder;
import com.sambo.inspection.bean.GlobleBean;
import com.sambo.inspection.bean.MessageBean;
import com.sambo.inspection.http.HttpClient;
import com.sambo.inspection.http.HttpResponseHandler;
import com.sambo.inspection.http.Urls;
import com.sambo.inspection.util.DateUtil;
import com.sambo.inspection.util.JsonUtil;
import com.sambo.inspection.util.SpUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageActivity extends BaseActivity {

    ImageView ivBack;
    ListView lvMessageList;

    private CommonAdapter<MessageBean> mAdapter;
    private List<MessageBean> mMessageBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);

        ivBack = findViewById(R.id.iv_back);
        lvMessageList = findViewById(R.id.lv_message_list);

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mMessageBeanList = new ArrayList<>();

        mAdapter = new CommonAdapter<MessageBean>(this, mMessageBeanList, R.layout.item_message) {
            @Override
            public void convert(ViewHolder helper, MessageBean item) {
                helper.setText(R.id.tv_message_title, item.getTitle());
                helper.setText(R.id.tv_message_content, item.getContent());

                if (item.getCreateDate().split(" ").length == 2) {
                    helper.setText(R.id.tv_message_date, item.getCreateDate().split(" ")[0]);
                } else {
                    helper.setText(R.id.tv_message_date, "");
                }

                if (TextUtils.equals("1", item.getImportance())) {
                    helper.setVisibility(R.id.iv_message_img, View.VISIBLE);
                } else {
                    helper.setVisibility(R.id.iv_message_img, View.GONE);
                }
            }
        };

        lvMessageList.setAdapter(mAdapter);

        lvMessageList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MessageActivity.this, MessageDetailActivity.class);
                intent.putExtra("msgModel", mMessageBeanList.get(i));
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        getMessageList();
    }

    private void getMessageList() {

        Map<String, String> params = new HashMap<>();

        params.put("state", "2");
        params.put("page", "1");
        params.put("limit", "100");

        HttpClient.get(this, Urls.API_SERVER_HOST + Urls.GET_MESSAGE, params,
                new HttpResponseHandler() {
                    @Override
                    public void onInnovationSuccess(JsonElement value) {
                        super.onInnovationSuccess(value);
                        MessageBean[] response = JsonUtil.toObj(value.toString(), MessageBean[].class);

                        mMessageBeanList.clear();

                        if (response != null && response.length > 0) {
                            SpUtil.setLastMessageTS(GlobleBean.getInstance().getUserName(), DateUtil.dateTime2TS(response[0].getCreateDate()));
                            mMessageBeanList.addAll(Arrays.asList(response));
                        } else {
                            SpUtil.setLastMessageTS(GlobleBean.getInstance().getUserName(), System.currentTimeMillis());
                        }

                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onInnovationCacheSuccess(JsonElement value) {
                        super.onInnovationCacheSuccess(value);
                        onInnovationSuccess(value);
                    }

                    @Override
                    public void onInnovationFailure(String msg) {
                        super.onInnovationFailure(msg);
                        SpUtil.setLastMessageTS(GlobleBean.getInstance().getUserName(), System.currentTimeMillis());
                    }
                }, new HttpClient.NetworkUnavailableListener() {
                    @Override
                    public void networkUnavailable() {
                        SpUtil.setLastMessageTS(GlobleBean.getInstance().getUserName(), System.currentTimeMillis());
                    }
                });

    }
}
